// Copyright 2013 Petar Maymounkov
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rh

import (
	"encoding/binary"
	"fmt"
	"hash/fnv"
)

// Q is a unique ID, reflecting on a file identity captured at a fixed point between updates.
type Q struct {
	ID  uint64
	Ver uint64
}

//
func (q Q) Hash64() uint64 {
	f := fnv.New64a()
	if err := binary.Write(f, binary.BigEndian, q); err != nil {
		panic("u")
	}
	return f.Sum64()
}

//
func (q Q) String() string {
	return fmt.Sprintf("(%.16x %d)", q.ID, q.Ver)
}

//
func QBytes(q []byte) uint64 {
	f := fnv.New64a()
	f.Write(q)
	return f.Sum64()
}

func QString(s string) uint64 {
	return QBytes([]byte(s))
}
